package com.yand.disk.demo.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yand.disk.demo.dao.TDownLinkMapper;
import com.yand.disk.demo.dao.TFileDownlinkMapper;
import com.yand.disk.demo.dao.TFileInfoMapper;
import com.yand.disk.demo.entity.Constant;
import com.yand.disk.demo.entity.TDownLink;
import com.yand.disk.demo.entity.TFileDownlink;
import com.yand.disk.demo.entity.TFileInfo;
import com.yand.disk.demo.entity.dto.FileLinkDto;
import com.yand.disk.demo.service.FileInfoService;
import com.yand.disk.demo.utils.DateUtils;
import com.yand.disk.demo.utils.NetDiskUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件相关service
 */
@Service
public class FileInfoServiceImpl implements FileInfoService {


    @Autowired
    private TFileInfoMapper fileInfoMapper;

    @Autowired
    private TDownLinkMapper downLinkMapper;

    @Autowired
    private TFileDownlinkMapper fileDownlinkMapper;

    /**
     * 查询所有的文件列表
     * @param fileInfo
     * @return
     */
    @Override
    public PageInfo<TFileInfo> queryFileList(TFileInfo fileInfo) {
        PageHelper.startPage(fileInfo.getPageNum(), fileInfo.getPageSize());

        return PageInfo.of(fileInfoMapper.selectFileInfos(fileInfo));
    }

    /**
     * 创建文件的下载链接
     *  1，将传回的fileIds 通过逗号 打散
     *  2，添加表（文件链接关联表）
     *  3，生成code 生成 提取码 生成链接
     * @param fileLinkDto
     */
    @Override
    public TDownLink createFileLink(FileLinkDto fileLinkDto) throws NoSuchAlgorithmException {
        TDownLink downLink =  TDownLink.builder().build();
        downLink.setLinkCode(NetDiskUtils.createLinkCode());
        String now  = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        downLink.setCreateTime(Timestamp.valueOf(now));
        downLink.setExpiretime(fileLinkDto.getExpiretime());
        downLink.setDownloadcount(fileLinkDto.getDownloadcount());
        downLink.setIscode(String.valueOf(fileLinkDto.getIscode()));
        if(fileLinkDto.getIscode() == Constant.IS_NEED_CODE){
            downLink.setIdentifyingcode(NetDiskUtils.createValiCode());
        }
        downLink.setLinkUrl("http://"+NetDiskUtils.getIpAndPort()+"/s/"+downLink.getLinkCode());
        downLinkMapper.insert(downLink);
        batchAddFileLink(downLink,fileLinkDto);
        return downLink;
    }

    /**
     * 批量插入文件链接关联关系表中
     * @param downLink
     * @param fileLinkDto
     */
    private void batchAddFileLink(TDownLink downLink, FileLinkDto fileLinkDto){
        String [] fileIdsArray = fileLinkDto.getFileIds().split(",");
        List<TFileDownlink> list = new ArrayList<>();
        for(String fileId : fileIdsArray){
            TFileDownlink fileDownlink = new TFileDownlink();
            fileDownlink.setFileid(Integer.parseInt(fileId));
            fileDownlink.setLinkid(downLink.getId());
            list.add(fileDownlink);
        }

        fileDownlinkMapper.batchInsert(list);
    }


}
